| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 
 | 
 import os
 import requests
 import socket
 import struct
 
 import logging
 
 
 logger = logging.getLogger('util')
 
 
 class IPAddresss:
 def __init__(self, ipdbFile):
 self.ipdb = open(ipdbFile, "rb")
 str = self.ipdb.read(8)
 (self.firstIndex, self.lastIndex) = struct.unpack('II', str)
 self.indexCount = int((self.lastIndex - self.firstIndex) / 7 + 1)
 
 
 def getVersion(self):
 s = self.getIpAddr(0xffffff00)
 return s
 
 def getAreaAddr(self, offset=0):
 if offset:
 self.ipdb.seek(offset)
 str = self.ipdb.read(1)
 (byte,) = struct.unpack('B', str)
 if byte == 0x01 or byte == 0x02:
 p = self.getLong3()
 if p:
 return self.getString(p)
 else:
 return ""
 else:
 self.ipdb.seek(-1, 1)
 return self.getString(offset)
 
 def getAddr(self, offset, ip=0):
 self.ipdb.seek(offset + 4)
 countryAddr = ""
 areaAddr = ""
 str = self.ipdb.read(1)
 (byte,) = struct.unpack('B', str)
 if byte == 0x01:
 countryOffset = self.getLong3()
 self.ipdb.seek(countryOffset)
 str = self.ipdb.read(1)
 (b,) = struct.unpack('B', str)
 if b == 0x02:
 countryAddr = self.getString(self.getLong3())
 self.ipdb.seek(countryOffset + 4)
 else:
 countryAddr = self.getString(countryOffset)
 areaAddr = self.getAreaAddr()
 elif byte == 0x02:
 countryAddr = self.getString(self.getLong3())
 areaAddr = self.getAreaAddr(offset + 8)
 else:
 countryAddr = self.getString(offset + 4)
 areaAddr = self.getAreaAddr()
 return countryAddr + " " + areaAddr
 
 def dump(self, first, last):
 if last > self.indexCount:
 last = self.indexCount
 for index in range(first, last):
 offset = self.firstIndex + index * 7
 self.ipdb.seek(offset)
 buf = self.ipdb.read(7)
 (ip, of1, of2) = struct.unpack("IHB", buf)
 address = self.getAddr(of1 + (of2 << 16))
 
 address = str(address, 'gbk').encode("utf-8")
 logger.info("%d %s %s" % (index, self.ip2str(ip), address))
 
 def setIpRange(self, index):
 offset = self.firstIndex + index * 7
 self.ipdb.seek(offset)
 buf = self.ipdb.read(7)
 (self.curStartIp, of1, of2) = struct.unpack("IHB", buf)
 self.curEndIpOffset = of1 + (of2 << 16)
 self.ipdb.seek(self.curEndIpOffset)
 buf = self.ipdb.read(4)
 (self.curEndIp,) = struct.unpack("I", buf)
 
 def getIpAddr(self, ip):
 L = 0
 R = self.indexCount - 1
 while L < R - 1:
 M = int((L + R) / 2)
 self.setIpRange(M)
 if ip == self.curStartIp:
 L = M
 break
 if ip > self.curStartIp:
 L = M
 else:
 R = M
 self.setIpRange(L)
 
 if ip & 0xffffff00 == 0xffffff00:
 self.setIpRange(R)
 if self.curStartIp <= ip <= self.curEndIp:
 address = self.getAddr(self.curEndIpOffset)
 
 address = str(address)
 else:
 address = "未找到该IP的地址"
 return address
 
 def getIpRange(self, ip):
 self.getIpAddr(ip)
 range = self.ip2str(self.curStartIp) + ' - ' \
 + self.ip2str(self.curEndIp)
 return range
 
 def getString(self, offset=0):
 if offset:
 self.ipdb.seek(offset)
 str = b''
 ch = self.ipdb.read(1)
 (byte,) = struct.unpack('B', ch)
 while byte != 0:
 str += ch
 ch = self.ipdb.read(1)
 (byte,) = struct.unpack('B', ch)
 return str.decode('gbk')
 
 def ip2str(self, ip):
 return str(ip >> 24) + '.' + str((ip >> 16) & 0xff) + '.' + str((ip >> 8) & 0xff) + '.' + str(ip & 0xff)
 
 def str2ip(self, s):
 (ip,) = struct.unpack('I', socket.inet_aton(s))
 return ((ip >> 24) & 0xff) | ((ip & 0xff) << 24) | ((ip >> 8) & 0xff00) | ((ip & 0xff00) << 8)
 
 def getLong3(self, offset=0):
 if offset:
 self.ipdb.seek(offset)
 str = self.ipdb.read(3)
 (a, b) = struct.unpack('HB', str)
 return (b << 16) + a
 
 
 
 def get_ipAddress(ip):
 """
 docstring
 """
 QQWRY_PATH = os.path.dirname(__file__) + "./data/qqwry.dat"
 ips = IPAddresss(QQWRY_PATH)
 return ips.getIpAddr(ips.str2ip(ip))
 
 def ipAPI(ip):
 """
 https://ip-api.com/docs/api:json
 """
 url="http://ip-api.com/json/{}".format(ip)
 data = requests.get(url).text
 return data
 
 if __name__ == "__main__":
 print(get_ipAddress("8.8.8.8"))
 
 
 |